package com.jgp.glmeeting.service.impl;

import com.jgp.glmeeting.mobile.commons.BaseResponse;
import com.jgp.glmeeting.model.Manager;
import com.jgp.glmeeting.repository.ManagerRepository;
import com.jgp.glmeeting.service.ManagerService;
import com.jgp.sys.common.query.OrderList;
import com.jgp.sys.common.query.QueryFilterList;
import com.jgp.sys.ui.Pager;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Collections;
import java.util.List;

@Service
public class ManagerServiceImpl implements ManagerService {

    @Autowired
    private ManagerRepository managerRepository;

    //添加会议总管
    @Transactional
    @Override
    public void addManager(Manager m) {managerRepository.createOrUpdate(m);
    }

    /**
     * 修改  因为涉及到权限表  所以修改要单独拿出来
     * @param m
     */


    //查询会议总管(分页)
    @Override
    public List<Manager> queryManager(Manager m, Pager pager) {

        OrderList orders = new OrderList();
        orders.addOrder("createDatetime","desc");
        String name=m.getName();
        QueryFilterList queryFilterList=new QueryFilterList();
        if(StringUtils.isNotBlank(name)){
            queryFilterList.addFilter("name","eq",name);
        }

        return managerRepository.read(queryFilterList,orders,pager);
    }



    //删除会议总管
    @Transactional
    @Override
    public void removeManager(String id) {
    managerRepository.delete(id);

    }

    //根据id查会议总管进行修改
    @Override
    public Manager queryID(String id) {
        return managerRepository.read(id);
    }

    //查所有给下拉列表用
    @Override
    public List<Manager> queryAll(Manager m) {
        OrderList orders = new OrderList();
        QueryFilterList queryFilterList=new QueryFilterList();
        orders.addOrder("createDatetime","desc");
        return managerRepository.read(queryFilterList,orders);
    }

    @Override
    public Manager queryManagerByMobilePhone(String mobilePhone) {
        QueryFilterList queryFilterList=new QueryFilterList();
        queryFilterList.addFilter("mobilePhone","eq",mobilePhone);
        List<Manager>lists=managerRepository.read(queryFilterList);
        if(lists.size()>0){
            return lists.get(0);
        }
        else{
            return null;
        }
    }

    @Override
    public Manager queryManagerByOpenId(String openId) {
        List<Manager>list=managerRepository.read(new QueryFilterList().addFilter("openId","eq",openId));
        if(CollectionUtils.isNotEmpty(list)){
            return list.get(0);
        }
        return null;
    }

    @Override
    public BaseResponse bindManager(String openId, String mobilePhone) {
        List<Manager>list=managerRepository.read(new QueryFilterList().addFilter("mobilePhone","eq",mobilePhone));
        if(CollectionUtils.isNotEmpty(list)){
            Manager manager=list.get(0);
            manager.setOpenId(openId);
            managerRepository.update(manager);
            BaseResponse response=new BaseResponse(200);
            response.setObj(manager);
            return response;
        }
        return null;
    }


}
